package com.qiyu.bigdata.reportexcel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

@Service
public class MailService {
    @Autowired
    JavaMailSenderImpl sender;


    public void sendEmail(String[] to, String subject,File attachment) throws MessagingException {

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(Objects.requireNonNull(sender.getUsername()));
        helper.setTo(to);
        helper.setSubject(attachment.getName());
        helper.setText(subject);
        helper.addAttachment(attachment.getName(), attachment);
        sender.send(message);
    }
}
